package com.alexsh.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Alexander Shmygin <AShmygin@luxoft.com>
 * @version 1.0
 */
public class CommonFormatterUtils {
    /**
     * dd/MM/yyyy.
     */
    public static final String MM_DD_YYYY = "MM/dd/yyyy";

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(MM_DD_YYYY);

    private static final Log LOG = LogFactory.getLog(CommonFormatterUtils.class);

    /**
     * The method converts date to "mm/dd/yyyy" String format.
     *
     * @param date the date to convert
     * @return the formatted string
     */
    public static String dateToStr(Date date) {
        String result = "";
        if (date == null) {
            return result;
        }
        synchronized (DATE_FORMAT) {
            try {
                result = DATE_FORMAT.format(date);
            } catch (Exception e) {
                LOG.error("Unable to format date: " + date, e);
            }
        }
        return result;
    }
}
